# modela2gcode.py (c) 2010 by William Grover <wgrover@gmail.com>
# http://wgrover.com

import modela_parser
from modela_parser import mu2mm
import gui

downHeight = None
upHeight = None
x = 0.0
y = 0.0
z = 0.0
down = False
up = False
verbose = False

infile = open(gui.pickFile(), 'r')
print "F120"  #works well for PTFE Teflon.
for line in infile.readlines(): 
    if verbose:  print line,
    # The following assumes that all Modela lines start with "echo", which is
    # the case if you're using a shell script to communicate with the Modela.
    # Lines that don't begin with "echo" are assumed to be comments and are
    # saved to the output G-Code file as comments (in parenthesis).  Any
    # existing parenthesis inside the Modela file will confuse things and are
    # replaced by [ ] brackets.  We then follow each comment line with a "M00"
    # G-code instruction so that the final G-Code program will pause after each
    # comment, waiting for the user to give the go-ahead.
    if not line.startswith("echo"):
        print "(" + line.strip().replace("(","[").replace(")","]") + ")"
        print "M00"
    for token in modela_parser.tokens.findall(line):
        token = token.replace(" ","")
        PU = modela_parser.PU.search(token)
        PD = modela_parser.PD.search(token)
        MC = modela_parser.MC.search(token)
        PR = modela_parser.PR.search(token)
        PA = modela_parser.PA.search(token)
        ZM = modela_parser.ZM.search(token)
        PZ = modela_parser.PZ.search(token)
        VS = modela_parser.VS.search(token)
        VZ = modela_parser.VZ.search(token)
        if PU:
            # PU:  Bring the Modela milling head up to upHeight and move at high
            # speed to the specified coordinates.
            z = upHeight
            print "  G00 X%f Y%f Z%f" % (x, y, z)
            if PU.groups()[0]:
                x = x + mu2mm(float(PU.groups()[0]))
            if PU.groups()[1]:
                y = y + mu2mm(float(PU.groups()[1]))
            print "  G00 X%f Y%f Z%f" % (x, y, z)
        elif PD:
            # PD:  Bring the Modela milling head down to downHeight and move at
            # FIXME speed to the specified coordinates.
            z = downHeight
            print "  G01 X%f Y%f Z%f" % (x, y, z)
            if PD.groups()[0]:
                x = x + mu2mm(float(PD.groups()[0]))
            if PD.groups()[1]:
                y = y + mu2mm(float(PD.groups()[1]))
            z = downHeight
            print "  G01 X%f Y%f Z%f" % (x, y, z)
        elif MC:
            pass
        elif PR:
            pass
        elif PA:
            pass
        elif ZM:
            # ZM:  Move the Modela milling head to the specified Z height.
            z = z + mu2mm(float(ZM.groups()[0]))
            print "  G01 X%f Y%f Z%f" % (x, y, z)
        elif PZ:
            # PZ:  Set the upHeight (safe, above workpiece) and downHeight
            # (milling) Z heights.
            downHeight = mu2mm(float(PZ.groups()[0]))
            upHeight = mu2mm(float(PZ.groups()[1]))
        elif VS:
            pass
        elif VZ:
            pass
        else:
            # We reached a token that the program can't parse; print it.
            print token

        